<template>
  <div class="app-container" >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="收件人" prop="userId">
<!--        <el-input v-model="form.userId" placeholder="请选择收件人" />-->
        <el-select v-model="form.userId" placeholder="请选择收件人" >
          <el-option
            v-for="item in UserEmail_List"
            :key="item.userId"
            :label="item.email"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="主题" prop="title" >
        <el-input v-model="form.title" placeholder="主题" />
      </el-form-item>
      <el-form-item label="邮件类型" prop="emailType" >
        <el-select v-model="form.emailType" placeholder="请选择邮件类型">
          <el-option
            v-for="dict in dict.type.email_type"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="附件" prop="file" >
        <file-upload v-model="form.file"/>
      </el-form-item>
      <el-form-item label="内容" prop="contentText" >
        <el-input   type="textarea" v-model="form.contentText" placeholder="" />
      </el-form-item>
      <el-form-item  v-show="false" label="email_genre" prop="email_genre">
      <el-input  v-model="form.emailGenre" placeholder=""/>
    </el-form-item>
      <el-form-item  v-show="false" label="ue_id" prop="ue_id">
        <el-input  v-model="form.ueId" placeholder=""/>
      </el-form-item>
    </el-form>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <span><h3>是否保存到草稿箱？</h3></span>
      <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveForm">保存</el-button>
      <el-button type="primary" @click="noSave">不保存</el-button>
      <el-button @click="quxiao">取 消</el-button>
      </span>
    </el-dialog>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">发 送</el-button>
      <el-button type="primary" @click="saveForm">存草稿</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </div>
</template>

<script>
import { addEmail, listUserEmail, selectOne } from '@/api/eamp/email_write'

export default {
  name: "Email_user",
  dicts: ['email_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户邮件关系表表格数据
      email_userList: [],
      //所有用户邮箱
      UserEmail_List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        emailId: null,
        connection: null,
        status: null,
        delFlg: null
      },
      // 表单参数
      form: {
        userId:null,
        emailGenre: 3,
      },
      // 表单校验
      rules: {
        userId: [{required: true, message: '请选择收件人', trigger: 'blur'}],
        title: [{required: true, message: '请输入主题名称', trigger: 'blur'}],
        emailType: [{required: true, message: '请选择邮件类型', trigger: 'blur'}],
        contentText: [{required: true, message: '请输入内容', trigger: 'blur'}],
      }
    };
  },
  created() {
    if (this.$route.query.ueId){
      this.form.ueId=this.$route.query.ueId
      this.queryParams.ueId=this.$route.query.ueId
      this.getList();
    }
    this.getUserEmail();
  },
  methods: {
    getUserEmail(){
      listUserEmail().then(response => {
        this.UserEmail_List = response.rows;
      })
    },
    /** 查询用户邮件关系表列表 */
    getList() {
      console.log(this.queryParams)
      selectOne(this.queryParams).then(response => {
        this.form=response.data;
        this.form.userId=response.data.consigneeId
        this.total = response.total;
        this.loading = false;
      });
    },
    noSave(){
      this.open = false;
      this.reset();
    },
    quxiao(){
      this.open = false;
    },
    // 取消按钮
    cancel() {
      this.open = true;
      this.title='离开提示';
    },
    // 表单重置
    reset() {
      this.form = {
        ueId: null,
        userId: null,
        emailId: null,
        connection: null,
        status: null,
        delFlg: null,
        emailGenre: 3
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户邮件关系表";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId!=null){
          this.form.emailGenre=null;
          addEmail(this.form).then(response => {
            this.$modal.msgSuccess("发送成功");
            this.reset();
            this.$router.push({path:'/email/email_outbox'});
          });
        }else {
            alert("请选择收件人")
          }
        }
      });
    },
    saveForm() {
      this.$refs["form"].validate(valid => {
        if (valid){
          if (this.form.userId!=null){
          addEmail(this.form).then(response => {
            this.$modal.msgSuccess("保存草稿成功");
            this.reset();
            this.$router.push({path:'/email/email_drafts'});
          });
        }else {
            alert("请选择收件人")
          }
        }
      });
    },
  }
};
</script>
